package com.hjq.fcloud.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.hjq.fcloud.R;


/**
 * 天使之所以会飞，是因为她把自己看的很轻...
 */

public class MaskView extends androidx.appcompat.widget.AppCompatImageView {
    private Context mContext;
    private int height;
    private int width;
    private float mDimension;
    private int mInsideColor;


    public MaskView(Context context) {
        super(context);
        this.mContext = context;
    }

    public MaskView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(context, 0);
    }


    public MaskView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(context, defStyleAttr);
    }


    private void init(Context context, int defStyleAttr) {

        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
// 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)


//        int devValue = screenWidth*400/360;

        TypedArray array = context.obtainStyledAttributes(defStyleAttr, R.styleable.MaskView);
        mDimension = array.getDimension(R.styleable.MaskView_myRadius, 400);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void draw(Canvas canvas) {
        Path path = new Path();
        path.addCircle(width / 2, height / 3, mDimension, Path.Direction.CW);
        canvas.clipPath(path, Region.Op.DIFFERENCE);

        Paint paint = new Paint();

        paint.setColor(Color.WHITE);
        canvas.drawRect(0, 0, width, height, paint);

        super.draw(canvas);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        width = widthSize;
        height = heightSize;

        setMeasuredDimension(width, height);
    }
}
